register({
  onUpdate: () => {
    emitUpdate({ isS: false });
  },
  onSuccess: () => {
    emitUpdate({ isS: true });
  },
  ver: 2,
  path: "/body_status"
});

// --------------
function emitUpdate(data) {
  var event = document.createEvent("Event");
  event.initEvent("sw.update", true, true);
  event.data = data;
  window.dispatchEvent(event);
}

function register(config) {
  if ("serviceWorker" in navigator) {
    const swUrl = `${config.path}/service-worker.js?v=${config.ver}`;
    navigator.serviceWorker
      .register(swUrl)
      .then(registration => {
        if (config && config.onRegster) {
          config.onRegster(registration);
        }

        registration.onupdatefound = () => {
          const installingWorker = registration.installing;
          if (installingWorker == null) {
            return;
          }
          installingWorker.onstatechange = () => {
            if (installingWorker.state === "installed") {
              if (navigator.serviceWorker.controller) {
                console.log(
                  "New content is available and will be used when all " +
                    "tabs for this page are closed. See http://bit.ly/CRA-PWA."
                );

                if (config && config.onUpdate) {
                  config.onUpdate(registration);
                }
              } else {
                console.log("Content is cached for offline use.");

                if (config && config.onSuccess) {
                  config.onSuccess(registration);
                }
              }
            }
          };
        };
      })
      .catch(error => {
        console.error("Error during service worker registration:", error);
      });
  }
}

function unregister() {
  if ("serviceWorker" in navigator) {
    navigator.serviceWorker.ready.then(registration => {
      registration.unregister();
    });
  }
}
